package com.csw.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
*/
public class MessageSend {
    public static void send(String phone, String code) throws Exception {

            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tLdG2JNh98e4Z6wMKUX", "f1z2ZnqfuYcwMKhD9VHafmbh50M9X8");
            IAcsClient client = new DefaultAcsClient(profile);
            //{"Message":"OK","RequestId":"BC392F95-EAF5-4E21-9DF7-20CCDB911B52","BizId":"448810875447501385^0","Code":"OK"}
            CommonRequest request = new CommonRequest();
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");
            request.putQueryParameter("RegionId", "cn-hangzhou");
            request.putQueryParameter("PhoneNumbers", phone);
            request.putQueryParameter("SignName", "imisbest");
            request.putQueryParameter("TemplateCode", "SMS_461505192");
            request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");

            CommonResponse response = client.getCommonResponse(request);
            String data = response.getData();
            JSONObject object = JSONObject.parseObject(data);
            if (object == null) {
                return;
            }
            String message = object.get("Message") == null ? "" : object.getString("Message");
            String rescode = object.get("Code") == null ? "" : object.getString("Code");
            if (!"OK".equals(rescode)) {
                System.out.println(phone + message);
            }

    }
}
